package com.example.communist_app_api.controller.web;

import com.example.communist_app_api.db.dto.PermissionDTO;
import com.example.communist_app_api.db.pojoDto.Role;
import com.example.communist_app_api.service.PermissionService;
import com.example.communist_app_api.service.RolePermissionService;
import com.example.communist_app_api.service.RoleService;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.web.req.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author love~mlm
 * @version 1.0
 * Create by 2022/12/26 18:40
 */
@RestController
@RequestMapping("/permission/role")
@Api("角色controller控制器")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    /*
    查询所有角色信息
     */
    @PostMapping("/")
    @ApiOperation("查询所有角色信息")
    public R selectAllRoleMessage(@RequestBody RoleShowReq req){
        //分页查询
        PageInfo<Role> pageInfo = roleService.findPage(req);
        return R.ok().put("pageInfo",pageInfo);
    }
    /*
    新增角色
     */
    @PostMapping("/create")
    @ApiOperation("新增角色")
    public R createNewRole(@RequestBody RoleCreateReq req){
        //新增角色
        roleService.createNewRole(req.getRoleName());
        return R.ok("新增角色成功");
    }
    /*
    修改角色--显示所有权限
     */
    @PostMapping("/showAssign")
    @ApiOperation("显示所有权限")
    public R assignRole(@RequestBody RoleIdShowAssignPermissionReq req){
        Integer roleId = req.getRoleId();
        if (roleId == null) {
            return R.error("获取角色Id失败");
        }else{
            //显示所有权限并进行分级
            List<PermissionDTO>  permissionDTOList = permissionService.getPermissionMenuList(req.getRoleId());
            //根据roleId获取角色已经拥有的权限,然后拥有的权限checked为true
            ArrayList<Integer> permissionIdByRoleId = rolePermissionService.getPermissionIdByRoleId(roleId);
            return R.ok().put("permissionList",permissionDTOList).put("permissionIdByRoleId",permissionIdByRoleId);
        }

    }
    /*
    修改角色--保存分配权限
     */
    @PostMapping("/updateAssignPermission")
    @ApiOperation("修改分配权限")
    public R updateAssignPermission(@RequestBody RoleUpdatePermissionReq req){
        //已勾选的集合
        List<Integer> permissionIdList = req.getPermissionIdList();
        Integer roleId = req.getRoleId();
        Boolean flag = rolePermissionService.updateRoleAssignPermission(permissionIdList,roleId);
        if (flag == true) {
            return R.ok("分配角色权限成功");
        }else {
            return R.error("分配权限失败");
        }
    }

    /*
    删除角色
     */
    @PostMapping("/delete")
    @ApiOperation("删除角色")
    public R deleteRole(@RequestBody RoleDeleteReq req){
        if (req.getRoleId() != null) {
            roleService.deleteRoleByRoleId(req.getRoleId());
        }else {
            R.error("角色Id为空");
        }

        return R.ok("删除角色成功");
    }

}
